{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Day1 变量与格式化字符串\n",
    "\n",
    "前面3天的内容都比较基础和简单，主要是带大家熟悉节奏，因为其实知识量是很大的，即使是战线拉长放在60天中我们都发现时间比较紧张，所以速成比较，代码学习就是需要时间积累的，编程是实践学科，不是理论学科，实践核心就在于堆时间。\n",
    "\n",
    "扎扎实实的跟下来，就是最大的捷径。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 一、了解变量与输出"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.1 变量的打印"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "a=1\n",
    "b=2\n",
    "c=3\n",
    "print(a)\n",
    "print(b)\n",
    "print(c)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "ipynb文件很好的一个点是，不用print也可以输出一个变量的值，但是仅限一个"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a=1\n",
    "b=2\n",
    "c=3\n",
    "a\n",
    "b\n",
    "c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 2 3\n"
     ]
    }
   ],
   "source": [
    "a,b,c=1,2,3 #多重赋值\n",
    "print(a,b,c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 2 3\n"
     ]
    }
   ],
   "source": [
    "print(a, b, c)  # 用逗号分隔多个参数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "思考 print(a b c) 可以么？  \n",
    "\n",
    "1. 观看报错提醒，学习用一个ai工具\n",
    "2. 简单理解下函数的概念，类比f（x，y，z）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "实际上a,b,c=1,2,3这个神奇的表达是我们python一个很难的知识点，叫做解包。对这些经典的表达记住，未来会有更深入的理解"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "a,b,c=1,2,3\n",
    "print(a, b, c, sep='\\n')  # 用换行符作为分隔符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'你好 World'"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = \"你好 World\"\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'你好'"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = '你好'\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'你好'"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = '''你好'''\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'我很开心因为我学会了Python'"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 字符串可以拼接\n",
    "a = \"我很开心\"\n",
    "b = \"因为我学会了Python\"\n",
    "c = a + b\n",
    "c "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.2 转义字符\n",
    "\n",
    "转义字符是以反斜杠 `\\` 开头的特殊字符,用于表示一些无法直接输入的字符或特殊功能。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第一行\n",
      "第二行\n",
      "第三行\n"
     ]
    }
   ],
   "source": [
    "# 1. \\n 换行符\n",
    "print(\"第一行\\n第二行\\n第三行\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "姓名  年龄  城市\n",
      "小明  18  北京\n",
      "张晓红  20  上海\n"
     ]
    }
   ],
   "source": [
    "# 使用空格对齐 - 不整齐\n",
    "print(\"姓名  年龄  城市\")\n",
    "print(\"小明  18  北京\")\n",
    "print(\"张晓红  20  上海\")  # 名字长度不同,就对不齐了"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " \\t 会自动调整间距,使得列对齐,就像 Excel 表格一样整齐"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "姓名\t年龄\t城市\n",
      "小明\t18\t北京\n",
      "张晓红\t20\t上海\n"
     ]
    }
   ],
   "source": [
    "# 2. \\t 制表符(Tab键)\n",
    "print(\"姓名\\t年龄\\t城市\")\n",
    "print(\"小明\\t18\\t北京\")\n",
    "print(\"张晓红\\t20\\t上海\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "还有其他几个转义字符我们留在后面说"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.3 格式化字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "数字和变量之间无法拼在一起，因为类型不同"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "我的名字是小明，我住在北京。\n"
     ]
    }
   ],
   "source": [
    "name = \"小明\"\n",
    "city = \"北京\"\n",
    "print(\"我的名字是\" + name + \"，我住在\" + city + \"。\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "我的名字是小明，我住在北京。\n"
     ]
    }
   ],
   "source": [
    "print(f\"我的名字是{name}，我住在{city}。\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "姓名: \"小明\", 城市: 北京\n"
     ]
    }
   ],
   "source": [
    "# 姓名两侧加上引号如何写？\n",
    "name = \"小明\"\n",
    "city = \"北京\"\n",
    "print(f'姓名: \"{name}\", 城市: {city}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "姓名: \"小明\"\n",
      "城市: 北京\n"
     ]
    }
   ],
   "source": [
    "print(f'姓名: \"{name}\"\\n城市: {city}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "你可能看一些比较老的代码中还有format和%这样的格式化方法，现在基本都不用了。知道即可"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 二、数值变量的基础运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "20 加 8 的结果是：28\n",
      "20 除以 8 的商是：2.5\n",
      "20 除以 8 的余数是：4\n"
     ]
    }
   ],
   "source": [
    "## 变量的基础运算\n",
    "num1 = 20\n",
    "num2 = 8\n",
    "\n",
    "a = num1 + num2  # 计算和\n",
    "b = num1 / num2  # 计算商\n",
    "c = num1 % num2  # 计算余数\n",
    "\n",
    "# 使用f-string分三行打印结果\n",
    "print(f\"{num1} 加 {num2} 的结果是：{a}\")\n",
    "print(f\"{num1} 除以 {num2} 的商是：{b}\") \n",
    "print(f\"{num1} 除以 {num2} 的余数是：{c}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最终价格是: 15.9\n",
      "节省金额是: 3.98\n"
     ]
    }
   ],
   "source": [
    "price = 19.9\n",
    "discount = 0.8\n",
    "\n",
    "final_price = price * discount  # 计算折扣后价格\n",
    "saved_amount = price - final_price  # 计算节省金额\n",
    "\n",
    "# 使用f-string分两行打印结果\n",
    "print(f\"最终价格是: {final_price:.2f}\")\n",
    "print(f\"节省金额是: {saved_amount:.2f}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "每日的作业请自行完成，这会强迫你利用AI工具，无需参考代码。\n",
    "\n",
    "几个学习思路：\n",
    "1. 可以让AI给你提供其他的实现思路\n",
    "2. 可以让AI继续给你出类似的题目"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "vs",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
